//
//  ViewController.m
//  02-分类界面
//
//  Created by peironggao on 2017/10/28.
//  Copyright © 2017年 gaopeirong. All rights reserved.
//

#import "PRClassificationVC.h"
#import "TTCategoryDao.h"
#import "TTCategory1Cell.h"
#import "TTCategory2Cell.h"
#import "TTCategory2CellHeaderView.h"


@interface PRClassificationVC ()<UITableViewDataSource, UITableViewDelegate, TTCategory2CellDelegate>

@property (nonatomic, strong) TTCategoryDao *categoryDao;

@property (nonatomic, weak) UITableView *category1TableView;
@property (nonatomic, strong) NSArray<TTCactegoryModel *> *categories;

@property (nonatomic, weak) UITableView *category2TableView;
@property (nonatomic, strong) TTCactegoryModel *selectCategory;

@end

@implementation PRClassificationVC
+ (instancetype)vc {
    return [[PRClassificationVC alloc] init];
}
- (TTCategoryDao *)categoryDao
{
    if (!_categoryDao)
    {
        _categoryDao = [[TTCategoryDao alloc] init];
    }
    return _categoryDao;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIView *category1ContainView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 65, self.view.bounds.size.height - 20)];
    [self.view addSubview:category1ContainView];
    category1ContainView.backgroundColor = CATEGORY1_DEFAULT_BG_COLOR;
    
    UITableView *category1TableView = [[UITableView alloc] init];
    category1TableView.backgroundColor = [UIColor clearColor];
    category1TableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    category1TableView.frame = CGRectMake(0, 20, category1ContainView.frame.size.width,
                                          self.view.bounds.size.height - 20);
    [category1ContainView addSubview:category1TableView];
    category1TableView.bounces = NO;
    category1TableView.dataSource = self;
    category1TableView.delegate = self;
    _category1TableView = category1TableView;
    
    UITableView *category2TableView = [[UITableView alloc] init];
    category2TableView.backgroundColor = CATEGORY1_SELECTED_BG_COLOR;
    category2TableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    category2TableView.frame = CGRectMake(CGRectGetMaxX(category1ContainView.frame),
                                          category1ContainView.frame.origin.y,
                                          self.view.frame.size.width - category1ContainView.frame.size.width,
                                          category1ContainView.frame.size.height);
    [self.view addSubview:category2TableView];
    category2TableView.dataSource = self;
    category2TableView.delegate = self;
    _category2TableView = category2TableView;
    NSLog(@"category2TableView2 = %.2f", category2TableView.frame.size.width);
    UIView *category2Header = [[UIView alloc] init];
    category2Header.frame = CGRectMake(0, 0, category2TableView.frame.size.width, 65);
    category2Header.backgroundColor = [UIColor clearColor];
    category2TableView.tableHeaderView = category2Header;
    
    UIButton *allBtn = [[UIButton alloc] init];
    allBtn.titleLabel.font = CELL_TITLE_FONT;
    [allBtn setBackgroundColor:[UIColor blueColor]];
    [allBtn setTitle:@"全部>>" forState:UIControlStateNormal];
    [allBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    allBtn.frame = CGRectMake(30, 20, 65, 35);
    [category2Header addSubview:allBtn];
    
    [category1TableView registerClass:[TTCategory1Cell class] forCellReuseIdentifier:@"TTCategory1Cell"];
    [category2TableView registerClass:[TTCategory2Cell class] forCellReuseIdentifier:@"TTCategory2Cell"];
    
    [self loadData];
}

- (void)loadData
{
    [self.categoryDao loadAllCategoriesComplete:^(NSArray<TTCactegoryModel *> *categories) {
        _categories = categories;
        _selectCategory = [_categories firstObject];
        [_selectCategory setSelected:YES];
        [_category1TableView reloadData];
        [_category2TableView reloadData];
    } fail:^(NSError *error) {
        NSLog(@"加载数据失败");
    }];
}

- (void)category2Cell:(TTCategory2Cell *)cell didClicked:(TTCactegoryModel *)model
{
    NSLog(@"点击了: %@", model.name);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == _category1TableView)
    {
        return 1;
    }
    else if (tableView == _category2TableView && _selectCategory != nil)
    {
        return _selectCategory.subCates.count;
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    if (tableView == _category1TableView)
    {
        return _categories.count;
    }
    else
    {
        return 1;
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _category1TableView)
    {
        TTCategory1Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"TTCategory1Cell"];
        TTCactegoryModel *model = [_categories objectAtIndex:indexPath.row];
        model.isLast = indexPath.row == (_categories.count - 1);
        cell.model = model;
        return cell;
    }
    else if (tableView == _category2TableView)
    {
        TTCategory2Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"TTCategory2Cell"];
        cell.tableView = tableView;
        cell.delegate = self;
        TTCactegoryModel *model = [_selectCategory.subCates objectAtIndex:indexPath.section];
        cell.model = model;
        return cell;
    }
    return nil;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    if (tableView == _category1TableView)
    {
        _selectCategory.selected = NO;
        _selectCategory = [_categories objectAtIndex:indexPath.row];
        _selectCategory.selected = YES;
        [_category1TableView reloadData];
        [_category2TableView reloadData];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _category2TableView)
    {
        TTCactegoryModel *model = [_selectCategory.subCates objectAtIndex:indexPath.section];
        if (model.cellHeight > 0)
        {
            return model.cellHeight;
        }
        TTCategory2Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"TTCategory2Cell"];
        cell.tableView = tableView;
        cell.model = model;
        return model.cellHeight;
    }
    else if (tableView == _category1TableView)
    {
        return 44;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView == _category2TableView)
        return 44;
    
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (tableView == _category2TableView)
    {
        TTCategory2CellHeaderView *view = [[TTCategory2CellHeaderView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        view.model = [_selectCategory.subCates objectAtIndex:section];
        return view;
    }
    return nil;
}


@end
